
import UtilFuncs from '../../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import LXCOM from '../../common/common.js'

var AbsorbMultiPos = {}

AbsorbMultiPos.calculate = function (mousePt, destPos) {
    var x = mousePt.x;
    var y = mousePt.y;
    var absobPt = { x, y };
    var posCount = destPos.posList.length;
    if (posCount < 2) {
        return absobPt;
    }

    //求当前鼠标点离矩形每个边最近的吸附点以及距离
    var prePt = destPos.posList[0];
    var mindis = -1;
    for (var i = 1; i < posCount; i++) {
        var nextPt = destPos.posList[i];
        var tmpPoint = { x, y };
        var dis = this.pt2LineSecDis(prePt.x, prePt.y, nextPt.x, nextPt.y, tmpPoint, mousePt);
        if (dis >= 0) {
            if (dis < mindis || mindis < 0) {
                mindis = dis;
                absobPt.x = tmpPoint.x;
                absobPt.y = tmpPoint.y;
            }
        }
        prePt = nextPt;
    }
    return absobPt;
}

AbsorbMultiPos.pt2LineSecDis = function (x1, y1, x2, y2, absobPt, mousePt) {
    let x = mousePt.x;
    let y = mousePt.y;
    var tmpPoint = { x, y };
      x = x1;
      y = y1;
    var pt1 = { x, y };

      x = x2;
      y = y2;
    var pt2 = { x, y };

    var linesec = new LXCOM.LGXLineSection();
    linesec.setPt(pt1, pt2);
    var dis = utilfuncs.pt2LineSec(mousePt, linesec, tmpPoint);
    absobPt.x = tmpPoint.x;
    absobPt.y = tmpPoint.y;
    return dis;
}

export default { AbsorbMultiPos }